package com.zhou.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhou.gulimall.product.dao.BrandDao;
import com.zhou.gulimall.product.entity.BrandEntity;
import com.zhou.gulimall.product.service.BrandService;
import com.zhou.gulimall.product.service.CategoryService;
import com.zhou.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.common.utils.PageUtils;
import com.zhou.common.utils.Query;

import com.zhou.gulimall.product.dao.CategoryBrandRelationDao;
import com.zhou.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zhou.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryService categoryService;

    @Autowired
    BrandService brandService;

    @Autowired
    BrandDao brandDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //根据id查询对应的名字
        categoryBrandRelation.setCatelogName(categoryService.getById(catelogId).getName());
        categoryBrandRelation.setBrandName(brandService.getById(brandId).getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
        categoryBrandRelation.setBrandId(brandId);
        categoryBrandRelation.setBrandName(name);
        this.update(categoryBrandRelation,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));

    }

    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
        categoryBrandRelation.setCatelogId(catId);
        categoryBrandRelation.setCatelogName(name);
        this.update(categoryBrandRelation,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));

    }

    @Override
    public List<BrandVo> BrandList(Long catId) {
        List<CategoryBrandRelationEntity> list = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandVo> brandVoList = new ArrayList<>();
        BrandEntity b = new BrandEntity();

        for (int i = 0; i < list.size(); i++) {
            BrandVo vo = new BrandVo();
           b = brandService.getOne(new QueryWrapper<BrandEntity>().eq(" brand_id",list.get(i).getBrandId()));
           vo.setBrandName(b.getName());
           vo.setBrandId(b.getBrandId());
          brandVoList.add(vo);
        }
        return brandVoList;

    }


}